/**
 * 告警相关 API
 */
import http from '../utils/request'

export interface Alert {
  id: number
  alert_number: string
  title: string
  content: string
  level: string
  alert_type: string
  device_id?: number
  device_code?: string
  device_name?: string
  rule_id?: number
  rule_name?: string
  is_resolved: boolean
  resolved_by?: number
  resolved_by_name?: string
  resolved_at?: string
  resolution_notes?: string
  source_data?: Record<string, any>
  created_at: string
  updated_at: string
}

export interface AlertListQuery {
  page?: number
  page_size?: number
  search?: string
  level?: string
  alert_type?: string
  device_id?: number
  is_resolved?: boolean
  rule_id?: number
  start_date?: string
  end_date?: string
  sort_by?: string
  sort_order?: string
}

export interface AlertRule {
  id: number
  rule_name: string
  description?: string
  alert_type: string
  level: string
  is_enabled: boolean
  condition_expression: string
  notification_template?: string
  notification_types: string[]
  notification_recipients: string[]
  check_interval: number
  max_alert_frequency: number
  device_types?: string[]
  locations?: string[]
  created_by: number
  created_by_name?: string
  created_at: string
  updated_at: string
  last_triggered_at?: string
  trigger_count: number
}

export interface AlertRuleCreate {
  rule_name: string
  description?: string
  alert_type: string
  level: string
  is_enabled?: boolean
  condition_expression: string
  notification_template?: string
  notification_types: string[]
  notification_recipients: string[]
  check_interval?: number
  max_alert_frequency?: number
  device_types?: string[]
  locations?: string[]
}

// 获取告警列表
export const getAlertList = (params?: AlertListQuery) => {
  return http.get('/alerts', { params })
}

// 获取告警详情
export const getAlertDetail = (id: number) => {
  return http.get(`/alerts/${id}`)
}

// 创建告警
export const createAlert = (data: {
  title: string
  content: string
  level: string
  alert_type: string
  device_id?: number
  rule_id?: number
  source_data?: Record<string, any>
}) => {
  return http.post('/alerts', data)
}

// 更新告警（解决告警）
export const updateAlert = (id: number, data: {
  is_resolved: boolean
  resolution_notes?: string
}) => {
  return http.patch(`/alerts/${id}`, data)
}

// 批量操作告警
export const batchOperateAlerts = (data: {
  alert_ids: number[]
  operation: string
  params?: Record<string, any>
}) => {
  return http.post('/alerts/batch-operation', data)
}

// 获取告警统计
export const getAlertStatistics = () => {
  return http.get('/alerts/statistics/dashboard')
}

// 获取告警规则列表
export const getAlertRuleList = (params?: {
  page?: number
  page_size?: number
  search?: string
  alert_type?: string
  level?: string
  is_enabled?: boolean
  sort_by?: string
  sort_order?: string
}) => {
  return http.get('/alerts/rules', { params })
}

// 获取告警规则详情
export const getAlertRuleDetail = (id: number) => {
  return http.get(`/alerts/rules/${id}`)
}

// 创建告警规则
export const createAlertRule = (data: AlertRuleCreate) => {
  return http.post('/alerts/rules', data)
}

// 更新告警规则
export const updateAlertRule = (id: number, data: Partial<AlertRuleCreate>) => {
  return http.put(`/alerts/rules/${id}`, data)
}

// 删除告警规则
export const deleteAlertRule = (id: number) => {
  return http.delete(`/alerts/rules/${id}`)
}

// 测试告警规则
export const testAlertRule = (data: {
  rule_id: number
  test_data?: Record<string, any>
}) => {
  return http.post(`/alerts/rules/${data.rule_id}/test`, data)
}